/**
 * www.lifexx.com Inc.
 * Copyright (c) 2014 All Rights Reserved.
 */
package net.mikoo.seals.boss.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.mikoo.seals.boss.biz.services.PartyServices;
import net.mikoo.seals.boss.biz.services.PersonServices;
import net.mikoo.seals.boss.biz.services.UserLoginServices;
import net.mikoo.seals.boss.common.BaseController;
import net.mikoo.seals.boss.common.WebContext;
import net.mikoo.seals.boss.common.exception.ParametersException;
import net.mikoo.seals.boss.model.Party;
import net.mikoo.seals.boss.model.Person;
import net.mikoo.seals.boss.model.UserLogin;

/***
 * 
 * @author huangxiaoming
 *
 */
@Controller
@RequestMapping("/admin/personal/")
public class PersonalController extends BaseController {

	Logger logger = Logger.getLogger(PersonalController.class);

	@Autowired
	private UserLoginServices userLoginServices;
	
	@Autowired
	private PartyServices partyServices;

	@Autowired
	private PersonServices personServices;

	@RequestMapping("/info")
	public String info(HttpServletRequest request, Model model) {

		Long partyId = this.getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		Person person = personServices.getByPartyId(partyId);
		model.addAttribute("person", person);

		return resultView(ADMIN_VIEW, "personal/info");
	}

	/**
	 * 修改操作员
	 * 
	 * @param userLogin
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/modify")
	@ResponseBody
	public Map<String, Object> modify(Person person,HttpServletRequest request, Model model) {

		try {
			
			personServices.modifyByPrimaryKey(person);
			
			Party loginParty = partyServices.loadByPrimaryKey(person.getPartyId());
			
			request.getSession().setAttribute(WebContext.ADMIN_LOGIN_SESSION_KEY, loginParty);
			
			
			return result(SUCCESS, "修改成功");

		} catch (ParametersException e) {
			logger.error("", e);
			return result(ERROR, e.getMessage());
		} catch (Exception e) {
			logger.error("", e);
			return result(ERROR, "系统出错");
		}
	}

	/**
	 * 修改密码
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/editPassword")
	public String editPassword(HttpServletRequest request, Model model) {

		UserLogin userLogin = getLoginUser(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		model.addAttribute("userLogin", userLogin);
		return resultView(ADMIN_VIEW, "personal/editPassword");
	}

	@RequestMapping("/modifyPassword")
	@ResponseBody
	public Map<String, Object> modifyPassword(HttpServletRequest request, Model model) {
		try {
			String userLoginId = getCurrentUserLoginId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);

			String currentPassword = request.getParameter("currentPassword");
			String newPassword = request.getParameter("newPassword");
			String confPassword = request.getParameter("confPassword");

			if (!newPassword.equals(confPassword)) {
				return result(ERROR, "两次密码输入不一致");
			}

			userLoginServices.changePassword(userLoginId, newPassword, currentPassword);

			removeSession(request, WebContext.ADMIN_LOGIN_SESSION_KEY);

			return result(SUCCESS, "修改成功");
		} catch (ParametersException e) {
			logger.error("", e);
			return result(ERROR, e.getMessage());
		} catch (Exception e) {
			logger.error("", e);
			return result(ERROR, "系统出错");
		}

	}

	@RequestMapping("/upportrait")
	public String upportrait(HttpServletRequest request, Model model) {

		Long partyId = this.getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		Person person = personServices.getByPartyId(partyId);
		model.addAttribute("person", person);
		return resultView(ADMIN_VIEW, "personal/upportrait");
	}

}
